package com.cos.hospital.ui.usercenter;

import android.text.Editable;
import android.view.View;
import android.widget.EditText;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.code.helper.KTextWatcher;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.URegular;
import com.cos.hospital.tool.UToast;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 新增助理
 * @Author: Kosmos
 * @Date: 2018年09月13日 21:52
 * @Email: KosmoSakura@foxmail.com
 */
public class AssistantAddActivity extends BaseActivity implements View.OnFocusChangeListener {
    @BindView(R.id.add_assis_phone)
    EditText edtPhone;
    @BindView(R.id.add_assis_name)
    EditText edtName;

    private String canSave;

    @Override
    protected int layout() {
        return R.layout.activity_assistant_add;
    }

    @Override
    protected void init() {
        setTitleLeftIcon();
        setTitleCenterTxt(getString(R.string.assistant_creat));
        setTitleRightTxt(getString(R.string.save));
        canSave = checkSavebtn();
    }

    /**
     * 3-15
     * 1.姓名栏，6-30个字符，只允许录入汉字，录入框失去焦点时自动验证是否符合规则，
     * 不符合则标题及录入框红色提示；
     * <p>
     * 2.手机号码格式为11位纯数字，录入框失去焦点时自动验证字符长度及手机号码规则是否符合，
     * 不符合则标题及录入框红色提示；
     * <p>
     * 3.录入符合规则后方可点击保存，保存时将给每个手机号码生成默认密码为：手机号码后6位；
     */
    @Override
    protected void logic() {
        canSave = checkSavebtn();
        edtName.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                canSave = checkSavebtn();
            }
        });
        edtPhone.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                canSave = checkSavebtn();
            }
        });
        edtName.setOnFocusChangeListener(this);
        edtPhone.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (!hasFocus) {
            switch (view.getId()) {
                case R.id.add_assis_name://校验名字:
                    if (!UTxt.isEmpty(edtName)) {
                        if (edtName.length() > 15) {
                            showNotice(getString(R.string.name_max_15));
                        }
                        if (!URegular.checkChineseLetter(edtName.getText().toString())) {
                            showNotice(getString(R.string.name_latter_about));
                        }
                    }
                    break;
                case R.id.add_assis_phone://校验手机
                    if (!UTxt.isEmpty(edtPhone) && !URegular.checkPhoneNum(edtPhone.getText().toString())) {
                        showNotice(getString(R.string.phone_number_error));
                    }
                    break;
            }
        }
    }

    private String checkSavebtn() {
        if (UTxt.isEmpty(edtName)) {
            return getString(R.string.please_enter_name);
        }
        if (edtName.length() > 15) {
            return getString(R.string.name_max_15);
        }
        if (!URegular.checkChineseLetter(edtName.getText().toString())) {
            return getString(R.string.name_latter_about);
        }
        if (UTxt.isEmpty(edtPhone.getText().toString())) {
            return getString(R.string.phone_number_enter);
        }
        if (!URegular.checkPhoneNum(edtPhone.getText().toString())) {
            return getString(R.string.phone_number_error);
        }
        return "";
    }

    @OnClick({R.id.title_back, R.id.title_next_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_next_txt://新增助理
                if (UTxt.isEmpty(canSave)) {
                    save();
                } else {
                    showNotice(canSave);
                }
                break;
        }
    }

    private void save() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldDoctorId", SPUtil.getUserId());
        jsonObject.addProperty("assistantName", edtName.getText().toString());
        jsonObject.addProperty("assistantPhone", edtPhone.getText().toString());
        UHttp.start(getServes().getMyAssistCreat(jsonObject), "新增助理", new HttpMsgListener<ResultBean>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean dto) {
                if (dto.isSuccess()) {
                    UToast.toast("保存成功");
                    setResult(1000);
                    finish();
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }


}
